import xlwt

from presentation.Apriori import Apriori, Rule_generate
from presentation.replace_goodsname import Replace_goodsname_rules
from presentation.main import  dataset

minsupports = [0.01,0.03,0.05,0.07,0.09] #0.03
minconfs = [0.15,0.25,0.35,0.45,0.55] #0.25

#创建excel文档
book = xlwt.Workbook(encoding='utf-8')

for i in range(len(minsupports)):
    print('最小支持度为：', minsupports[i])
    # 添加sheet
    sheet = book.add_sheet('最小支持度为%s' % (minsupports[i]), cell_overwrite_ok=True)
    # 写入表格的列标题
    sheet.write(0, 0, '最大频繁项集')  # 0，0为坐标
    sheet.write(0, 1, '最小置信度')
    sheet.write(0, 2, '关联规则')

    Lmax = Apriori(dataset, minsupports[i])
    # finalLmax = Replace_goodsname_Lmax(Lmaxs)
    # 写入最大频繁项集Lmax
    sheet.write(1, 0, str(Lmax))

    for j in range(len(minconfs)):
        # 写入最小支持数minconfs
        sheet.write(j + 1, 1, minconfs[j])
        rules = Rule_generate(dataset, Lmax, minconfs[j])
        finalRules = Replace_goodsname_rules(rules)
        # 写入关联规则
        sheet.write(j + 1, 2, str(finalRules))
        print('*' * 90)
    print('--' * 50)

book.save(r'D:\数据仓库挖掘presentation\商品关联规则对比.xls')
print('finish writing!!')